<?php

class Model_Orm_Movie extends \Orm\Model
{
    protected static $_primary_key = array('id');
    protected static $_properties = array(
            'id',
            'title_lv' => array(
                'validation' => array(
                    'required',
                    'min_length' => array(1),
                    'max_length' => array(200)
                )
            ),
            'title_en' => array(
                'validation' => array(
                    'required',
                    'min_length' => array(1),
                    'max_length' => array(200)
                )
            ),
            'title_original' => array(
                'validation' => array(
                    'required',
                    'min_length' => array(1),
                    'max_length' => array(200)
                )
            ),
            'description_lv' => array(
                'validation' => array(
                    'required',
                    'min_length' => array(1),
                    'max_length' => array(5000)
                )
            ),
            'description_en' => array(
                'validation' => array(
                    'required',
                    'min_length' => array(1),
                    'max_length' => array(5000)
                )
            ),
            'year' => array(
                'validation' => array(
                    'required',
                    'numeric_min' => array(1850),
                    'numeric_max' => array(2020)
                )
            ),
            'age_restriction' => array(
                'validation' => array(
                    'numeric_min' => array(1),
                    'numeric_max' => array(100)
                )
            ),
            'duration' => array(
                'validation' => array(
                    'required',
                    'numeric_min' => array(1),
                    'numeric_max' => array(1000)
                )
            ),
            'video_id' => array(
                'validation' => array(
                    'required',
                    'min_length' => array(5),
                    'max_length' => array(500)
                )
            ),
            'poster_large',
            'poster_small'
    );

    protected static $_has_many = array(
	'genres' => array(
	    'key_from' => 'id',
	    'model_to' => 'Model_Orm_Moviegenre',
	    'key_to' => 'movie_id',
	    'cascade_save' => true,
	    'cascade_delete' => false),
	'shows' => array(
	    'key_from' => 'id',
	    'model_to' => 'Model_Orm_Show',
	    'key_to' => 'movie_id',
	    'cascade_save' => true,
	    'cascade_delete' => true),
	'reviews' => array(
	    'key_from' => 'id',
	    'model_to' => 'Model_Orm_Review',
	    'key_to' => 'movie_id',
	    'cascade_save' => true,
	    'cascade_delete' => true)
    );
    
    protected static $_table_name = 'movies';

    public static function validate($factory) {
	$val = Validation::forge($factory);

	$val->add_field('Title_Lv', 'Nosaukums latviski', 'required|max_length[200]|min_length[1]');
        $val->add_field('Title_En', 'Nosaukums angliski', 'required|max_length[200]|min_length[1]');
        $val->add_field('Title_Original', 'Nosaukums oriģinālvalodā', 'required|max_length[200]|min_length[1]');
        $val->add_field('Year', 'Izdošanas gads', 'required|numeric_min[1900]|numeric_max[2020]');
        $val->add_field('Description_Lv', 'Apraksts latviski', 'required|max_length[5000]|min_length[1]');
        $val->add_field('Description_En', 'Apraksts angliski', 'required|max_length[5000]|min_length[1]');
        $val->add_field('Trailer_Link', 'Filmas treileris', 'max_length[500]|min_length[5]');
        $val->add_field('Duration', 'Filmas ilgums', 'required|numeric_min[1]|numeric_max[1000]');
        $val->add_field('Age_Restriction', 'Vecuma ierobežojums', 'numeric_min[0]|numeric_max[100]');
    //    $val->add_field('Poster', 'Filmas plakāts', 'required');

	return $val;
    }
}
